package com.ruoyi.datastatistics.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.datastatistics.mapper.PublicShareReportMapper;
import com.ruoyi.datastatistics.domain.PublicShareReport;
import com.ruoyi.datastatistics.service.IPublicShareReportService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
/**
 * 公摊报表Service业务层处理
 *
 * @author zt
 * @date 2025-05-27
 */
@Service
public class PublicShareReportServiceImpl extends ServiceImpl<PublicShareReportMapper, PublicShareReport> implements IPublicShareReportService
{
    @Autowired
    private PublicShareReportMapper publicShareReportMapper;

    /**
     * 查询公摊报表
     *
     * @param id 公摊报表主键
     * @return 公摊报表
     */
    @Override
    public PublicShareReport selectPublicShareReportById(Long id)
    {

        return publicShareReportMapper.selectById(id);
    }

    /**
     * 查询公摊报表列表
     *
     * @param publicShareReport 公摊报表
     * @return 公摊报表
     */
    @Override
    public List<PublicShareReport> selectPublicShareReportList(PublicShareReport publicShareReport)
    {
        LambdaQueryWrapper<PublicShareReport> queryWrapper = new LambdaQueryWrapper<>();
        if(!StringUtils.isEmpty(publicShareReport.getTenantId())){
            queryWrapper.eq(PublicShareReport::getTenantId,publicShareReport.getTenantId());
        }
        if(!StringUtils.isEmpty(publicShareReport.getPublicShareId())){
            queryWrapper.eq(PublicShareReport::getPublicShareId,publicShareReport.getPublicShareId());
        }
        if(!StringUtils.isEmpty(publicShareReport.getPublicShareName())){
            queryWrapper.eq(PublicShareReport::getPublicShareName,publicShareReport.getPublicShareName());
        }
        if(publicShareReport.getEnergyType() != null){
            queryWrapper.eq(PublicShareReport::getEnergyType,publicShareReport.getEnergyType());
        }
        if(publicShareReport.getPublicShareAmount() != null){
            queryWrapper.eq(PublicShareReport::getPublicShareAmount,publicShareReport.getPublicShareAmount());
        }
        if(publicShareReport.getPublicShareCost() != null){
            queryWrapper.eq(PublicShareReport::getPublicShareCost,publicShareReport.getPublicShareCost());
        }
        if(publicShareReport.getPrice() != null){
            queryWrapper.eq(PublicShareReport::getPrice,publicShareReport.getPrice());
        }
        if(!StringUtils.isEmpty(publicShareReport.getReportDate())){
            queryWrapper.eq(PublicShareReport::getReportDate,publicShareReport.getReportDate());
        }
        if(publicShareReport.getReportType() != null){
            queryWrapper.eq(PublicShareReport::getReportType,publicShareReport.getReportType());
        }
        if(!StringUtils.isEmpty(publicShareReport.getCreatedBy())){
            queryWrapper.eq(PublicShareReport::getCreatedBy,publicShareReport.getCreatedBy());
        }
        if(publicShareReport.getCreatedTime() != null){
            queryWrapper.eq(PublicShareReport::getCreatedTime,publicShareReport.getCreatedTime());
        }
        if(!StringUtils.isEmpty(publicShareReport.getUpdatedBy())){
            queryWrapper.eq(PublicShareReport::getUpdatedBy,publicShareReport.getUpdatedBy());
        }
        if(publicShareReport.getUpdatedTime() != null){
            queryWrapper.eq(PublicShareReport::getUpdatedTime,publicShareReport.getUpdatedTime());
        }
        return publicShareReportMapper.selectList(queryWrapper);
    }

    /**
     * 新增公摊报表
     *
     * @param publicShareReport 公摊报表
     * @return 结果
     */
    @Override
    public int insertPublicShareReport(PublicShareReport publicShareReport)
    {
        return publicShareReportMapper.insert(publicShareReport);
    }

    /**
     * 新增公摊报表----水的公摊（生产）
     * kyx month 参数不用
     * @return
     */
    @Override
    public int insertPublicShareReportForWaterTerminals(String frequencyType, String month, String freezeTime,String userId)
    {
        if("1".equals(frequencyType)){
            return publicShareReportMapper.insertForWaterTerminalsForOne("1",month,freezeTime,"0");
        }else if("3".equals(frequencyType)){
            return publicShareReportMapper.insertForWaterTerminalsForThree("1",month.substring(0,7),freezeTime,userId);
        }else {
            return 0;
        }
    }
    /**
     * 新增公摊报表----电的公摊（生产）
     * kyx
     * @return
     */
    @Override
    public int insertPublicShareReportForElectricityTerminals(String frequencyType, String month, String freezeTime,String userId)
    {
        if("1".equals(frequencyType)){
            return publicShareReportMapper.insertForElectricityTerminalsForOne("1",month,freezeTime,"0");
        }else if("3".equals(frequencyType)){
            return publicShareReportMapper.insertForElectricityTerminalsForThree("1",month.substring(0,7),freezeTime,userId);
        }else {
            return 0;
        }

    }
    /**
     * 修改公摊报表
     *
     * @param publicShareReport 公摊报表
     * @return 结果
     */
    @Override
    public int updatePublicShareReport(PublicShareReport publicShareReport)
    {
        return publicShareReportMapper.updateById(publicShareReport);
    }

    /**
     * 批量删除公摊报表
     *
     * @param ids 需要删除的公摊报表主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deletePublicShareReportByIds(Long[] ids)
    {
        return publicShareReportMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除公摊报表信息
     *
     * @param id 公摊报表主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deletePublicShareReportById(Long id)
    {
        return publicShareReportMapper.deleteById(id);
    }

    @Override
    public List<PublicShareReport> selectTotalReportList(PublicShareReport publicShareReport) {
        return publicShareReportMapper.selectTotalReportList(publicShareReport);
    }

    @Override
    public List<PublicShareReport> selectProductReportList(PublicShareReport publicShareReport) {
        if(publicShareReport.getEnergyType() == 1){
            //电
            return publicShareReportMapper.selectProductReportList1(publicShareReport);
        }else if(publicShareReport.getEnergyType() == 2){
            //水
            return publicShareReportMapper.selectProductReportList2(publicShareReport);
        }
        return new ArrayList<>();
    }

    @Override
    public BigDecimal selectProductReportListSum(PublicShareReport publicShareReport) {
        if(publicShareReport.getEnergyType() == 1){
            //电
            return publicShareReportMapper.selectProductReportList1Sum(publicShareReport);
        }else if(publicShareReport.getEnergyType() == 2){
            //水
            return publicShareReportMapper.selectProductReportList2Sum(publicShareReport);
        }
        return BigDecimal.ZERO;
    }
}
